var express = require('express');
var router = express.Router();
// New Code
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/TaoProject');

/* 新增用户 */
router.post('/', function (req, res, next) {
    // Get our form values. These rely on the "name" attributes
    var userName = req.body.username;
    var age = req.body.age;
    var sex = req.body.sex;
    var school = req.body.school;
    var hobby1 = req.body.hobby1;
    var hobby2 = req.body.hobby2;
    var hobby3 = req.body.hobby3;

    // Set our collection
    var collection = db.get('usercollection');

    // Submit to the DB
    collection.insert({
        "username": userName,
        "userInfo": {
            "age" : age,
            "sex" : sex,
            "school" : school
        },
        "userHobby" : [hobby1,hobby2,hobby3]
    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem adding the information to the database.");
        } else {
            // If it worked, set the header so the address bar doesn't still say /adduser
            res.location("userlist");
            // And forward to success page
            res.redirect("userlist");
        }
    });

});

module.exports = router;